import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chip/bean/User.dart';
import 'package:flutter_chip/config/Api.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:flutter_chip/config/Http.dart';
import 'package:flutter_chip/config/Routers.dart';
import 'package:flutter_chip/utils/toast_utils.dart';
import 'package:flutter_chip/widget/widget_comm.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

///粉丝&关注的人列表
class UserListPage extends StatefulWidget {
  final int type;
  final String userId;
  final isSelf;

  const UserListPage(this.type, this.userId,this.isSelf);

  @override
  _UserListPageState createState() => _UserListPageState();
}

class _UserListPageState extends State<UserListPage> {
  var _item = [];
  bool _loading = false;

  @override
  void initState() {
    super.initState();
    _initDatas();
  }

  @override
  Widget build(BuildContext context) {
    String title = widget.type == 1 ? "我关注的人" : "我的粉丝";
    if (widget.isSelf) {
      title=widget.type==1?"我的关注":"我的粉丝";
    }else{
      title=widget.type==1?"TA的关注":"TA的粉丝";
    }
    return Scaffold(
      appBar: AppBar(
        elevation: 1.0,
        backgroundColor: Theme.of(context).canvasColor,
        title: Text(title,style: TextStyle(fontSize: 18)),
      ),
      body: _item.length > 0
          ? ListView.separated(
              itemCount: _item.length,
              physics: AlwaysScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return _itemView(context, index);
              },
              separatorBuilder: _dividerWidget,
            )
          : _loadWidget(),
    );
  }

  Widget _itemView(BuildContext context, int index) {
    User user = _item[index];
    Widget headWidget = ClipOval(
        child: CachedNetworkImage(
            width: 36,
            height: 36,
            imageUrl: Api.getUrl(user.imgUrl),
            errorWidget: (context, url, error) =>
                Image.asset(Comm.IMG_DEFAULT, fit: BoxFit.cover),
            fit: BoxFit.cover));
    return Container(
        color: Theme.of(context).canvasColor,
        height: 56,
        child: Row(children: <Widget>[
          GestureDetector(
            onTap: () {
              Routers.router.navigateTo(
                  context, "${Routers.PAGE_USERINFO}?userId=${user.userId}",
                  transition: TransitionType.inFromRight);
            },
            child: Container(
              height: 56,
              alignment: Alignment.center,
              margin: EdgeInsets.all(10),
              child: headWidget,
            ),
          ),
          Text(
            '${user.userName}',
            style: TextStyle(fontSize: 16),
          ),
          Expanded(
            child: Container(),
          ),
          GestureDetector(
            onTap: () {},
            child: Container(
              width: 60,
              height: 30,
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: 10),
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.lightBlue),
                  borderRadius: BorderRadius.circular(4)),
              child: Text(
                widget.type == 1 ? '已关注' : '+关注',
                style: TextStyle(color: Colors.lightBlue),
              ),
            ),
          )
        ]));
  }

  ///加载中和空界面
  Widget _loadWidget() {
    if (_loading) {
      return Container(
        alignment: Alignment.center,
        child: SpinKitCircle(
          color: Theme.of(context).accentColor,
        ),
      );
    } else {
      String msg = widget.type == 1 ? "您还没有关注的人，去寻找牛人关注他吧" : "没有粉丝哦";
      return CommWidget.nullWidget(msg: msg);
    }
  }

  ///分隔线
  Widget _dividerWidget(BuildContext context, int index) {
    return new Divider(
      height: 1.0,
      color: Colors.grey,
    );
  }

  ///获取数据
  void _initDatas() {
    if (_loading) return;
    setState(() {
      _loading = true;
    });
    String url =
        '${widget.type == 1 ? Api.URL_FOLLOW_LIST : Api.URL_FOLLOW_FANS}?userId=${widget.userId}';
    Http.get(url, successCallBack: (data) {
      List list = json.decode(data).map((m) => User.fromJson(m)).toList();
      setState(() {
        _item.addAll(list);
        _loading = false;
      });
    }, errorCallBack: (msg) {
      Toast.show(msg);
      setState(() {
        _loading = false;
      });
    });
  }
}
